Introduction to CSharp
Introduction to C# This page is an idiosyncratic introduction to C# tailored to the needs of this GREAT Computing. Other introductions to C# include: * TODO: Add some introductions. Mono is being used as the CLR and gmcs as the compiler in these examples, see http://www.mono-project.com/Main_Page and http://www.go-mono.com/docs/ Hello GREAT Computing Based on Kernigham and Ritchie. using System; namespace GreatC.Introduction { /// /// How to develop console applications for GREAT Computing. /// public class ConsoleApplication { /// /// The entry point for the application. /// public static void Main(string[] args) { Console.WriteLine("Hello GREAT Computing!"); } } } Compile and Run with Mono Compile and run this: jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ gmcs ConsoleApplication.cs jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ mono ConsoleApplication.exe Hello GREAT Computing! jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ Now that we've written compiled and run our first program we could go on in a similar vein, but we should stop and ask how can we write a specification for this program, prove the program meets the specification and write the program with tests that raise our confidence in its correctness? We will work towards a formal specification by looking at testing this simple application. Complie and Run with C# C:\GreatCWiki\GreatC\Introduction>csc ConsoleApplication.cs . . . . . . TODO C:\GreatCWiki\GreatC\Introduction>ConsoleApplication.exe Hello GREAT Computing! Fixing: warning CS1668: Invalid search path This issue if being looked at as part of Treat warnings as errors (REFERENCE). These comments are for "Microsoft Windows Server 2003". When doing the initial compilation with csc get the following: C:\GreatCWiki\GreatC\Introduction>csc ConsoleApplication.cs Microsoft ® Visual C# 2008 Compiler version 3.5.30729.1 for Microsoft ® .NET Framework version 3.5 Copyright © Microsoft Corporation. All rights reserved. warning CS1668: Invalid search path 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib' specified in 'LIB environment variable' -- 'The system cannot find the path specified. ' The Target: for the Visual Studio Command Prompt is: %comspec% /k ""c:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86 I think the following file is being called: C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat The path is being set indirectly at the moment and some time analysing the the bat file will be needed. The path C:\Program Files\Microsoft SDKs\Windows\v6.0A exists. The lib directory is missing. * Try adding in the lib directory. This fixes the warning for the moment. MOVE THIS: Building an NUnit Test Fixture C:\GreatCWiki\GreatC\EduRegex>csc /t:library RegexTests.cs ^ MOre? /r:"C:\Program Files\NUnit 2.5\bin\net-2.0\framework\nunit.framework.dll" Microsoft ® Visual C# 2008 Compiler version 3.5.30729.1 for Microsoft ® .NET Framework version 3.5 Copyright © Microsoft Corporation. All rights reserved. C:\GreatCWiki\GreatC\EduRegex> Testing Hello GREAT Computing The console is a file device and we can pipe its output to a file: jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ mono ConsoleApplication.exe Hello GREAT Computing! jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ mono ConsoleApplication.exe > actual.txt jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ more actual.txt Hello GREAT Computing! jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ We call this file actual.txt as this is the output we get when we run the application. We can now compare this output to an expected output. We simply create a file with the expected output. Hello GREAT Computing! -------- expected.txt We then compare the two files. jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ diff expected.txt actual.txt jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ The two files are the same. We can put all of the above into a test file. #!/bin/bash mono ConsoleApplication.exe > actual.txt diff expected.txt actual.txt At the moment this file returns nothing as the following run shows: jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ Tests/hellotest.sh jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ If our application is wrong we will see the difference between the expected and actual results. Change the application as follows: Console.WriteLine("Hello Computing!"); Compile and run the test. jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ Tests/hellotest.sh 1c1 < Hello GREAT Computing! --- > Hello Computing! jason@U810-64-T630:~/work/greatc/edu/csharp/GreatC/Introduction$ A complete build and test: #!/bin/bash gmcs ConsoleApplication.cs mono ConsoleApplication.exe > actual.txt if diff expected.txt actual.txt > /dev/null then echo PASS else echo FAIL fi Console Specification Would it be useful to look at files in Bowen's book? Category:Introduction Category:Education Category:CSharp